package com.hyb.controller;


import com.alibaba.fastjson.JSON;
import com.hyb.Vo.StockVo;
import com.hyb.entity.Product;
import com.hyb.entity.Stock;
import com.hyb.page.PageReq;
import com.hyb.response.Result;
import com.hyb.service.ProductService;
import com.hyb.service.StockService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  库存Controller
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/stock")
public class StockController {
    @Autowired
    private StockService stockService;

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/createStock",method = RequestMethod.POST)
    @ApiOperation("新建仓库信息")
    public String createStock(@RequestBody StockVo stockVo){

        Product product = productService.selectProductByVo(stockVo.getProductSeries(), stockVo.getProductModel(), stockVo.getProductBatch());

        if (product == null){
            return JSON.toJSONString(new Result().setCode(401).setMessage("无此产品，请核对后重新填写"));
        }else {
            Stock stock = new Stock();
            stock.setProductId(product.getProductId());
            stock.setStoreName(stockVo.getStoreName());
            stock.setCargoNum(stockVo.getCargoNum());
            stock.setCargoSpace(stockVo.getCargoSpace());
            stock.setCargoRemarks(stockVo.getCargoRemarks());
            stockService.createStockInfo(stock);
            return JSON.toJSONString(new Result().setCode(200).setMessage("保存成功"));
        }
    }

    @RequestMapping(value = "/loadStocks",method = RequestMethod.GET)
    @ApiOperation("加载仓库信息")
    public String loadStocks(Integer limit, Integer current, String productType, String cargoName){
        PageReq stocks = stockService.getStockS(limit, current, productType, cargoName);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(stocks));
    }

    @RequestMapping(value = "/loadStockByStockId",method = RequestMethod.GET)
    @ApiOperation("通过仓库编号加载仓库信息")
    public String loadStockByStockId(Integer stockId){
        StockVo stockByStockId = stockService.getStockByStockId(stockId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(stockByStockId));
    }

    @RequestMapping(value = "/updateStock",method = RequestMethod.PUT)
    @ApiOperation("更新仓库信息")
    public String updateStock(@RequestBody StockVo stockVo){
        Product product = productService.selectProductByVo(stockVo.getProductSeries(), stockVo.getProductModel(), stockVo.getProductBatch());

        if (product == null){
            return JSON.toJSONString(new Result().setCode(401).setMessage("无此产品，请核对后重新填写"));
        }else {
            Stock stock = new Stock();
            stock.setStockId(stockVo.getStockId());
            stock.setProductId(product.getProductId());
            stock.setStoreName(stockVo.getStoreName());
            stock.setCargoNum(stockVo.getCargoNum());
            stock.setCargoSpace(stockVo.getCargoSpace());
            stock.setCargoRemarks(stockVo.getCargoRemarks());
            stockService.updateStock(stock);
            return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
        }
    }

    @RequestMapping(value = "/deleteStock",method = RequestMethod.DELETE)
    @ApiOperation("删除仓库信息")
    public String deleteStock(Integer stockId){
        stockService.deleteStock(stockId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("删除成功"));
    }
}
